package controllers

import (
	"encoding/json"
	"supershare/backend/models"

	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/server/web"
)

// BookingSettingController 预约配置控制器
type BookingSettingController struct {
	web.Controller
}

// GetAll godoc
// @Summary      Get all booking settings
// @Description  get all booking settings
// @Tags         booking_setting
// @Accept       json
// @Produce      json
// @Success      200  {array}  models.BookingSetting
// @Router       /booking_setting [get]
func (c *BookingSettingController) GetAll() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}
	franchiseID := permResult.Admin.FranchiseId
	storeID, _ := c.GetInt64("store_id", 0)
	var settings []models.BookingSetting
	var err error

	if storeID > 0 {
		// Check if the store belongs to the franchise
		store, err_store := models.GetStoreById(storeID)
		if err_store != nil || store.FranchiseId != franchiseID {
			c.Data["json"] = map[string]interface{}{"success": false, "message": "无权访问该门店的配置"}
			c.ServeJSON()
			return
		}

		setting, err_get := models.GetBookingSettingByStoreId(storeID)
		if err_get != nil {
			if err_get != orm.ErrNoRows {
				err = err_get
			}
		} else {
			settings = append(settings, *setting)
		}
	} else {
		settings, err = models.GetBookingSettingsByFranchiseId(franchiseID)
	}

	if err != nil {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "获取配置列表失败: " + err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{"success": true, "data": settings}
	c.ServeJSON()
}

// GetByStoreId godoc
// @Summary      Get booking setting by store id
// @Description  get booking setting by store id
// @Tags         booking_setting
// @Accept       json
// @Produce      json
// @Param        store_id  path   int  true  "Store ID"
// @Success      200  {object}  models.BookingSetting
// @Router       /booking_setting/{store_id} [get]
func (c *BookingSettingController) GetByStoreId() {
	storeId, _ := c.GetInt64(":store_id")
	if storeId == 0 {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "无效的门店ID"}
		c.ServeJSON()
		return
	}

	setting, err := models.GetBookingSettingByStoreId(storeId)
	if err != nil {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "获取配置失败: " + err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{"success": true, "data": setting}
	c.ServeJSON()
}

// Update godoc
// @Summary      Update booking setting
// @Description  update booking setting
// @Tags         booking_setting
// @Accept       json
// @Produce      json
// @Param        body  body   models.BookingSetting  true  "The booking setting content"
// @Success      200  {object}  map[string]interface{}
// @Router       /booking_setting [put]
func (c *BookingSettingController) Update() {
	var setting models.BookingSetting
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &setting)
	if err != nil {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "无效的请求参数"}
		c.ServeJSON()
		return
	}

	if setting.StoreId == 0 {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "门店ID不能为空"}
		c.ServeJSON()
		return
	}

	_, err = models.CreateOrUpdateBookingSetting(&setting)
	if err != nil {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "更新配置失败: " + err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{"success": true, "message": "更新成功"}
	c.ServeJSON()
}

// Delete godoc
// @Summary      Delete booking setting
// @Description  delete booking setting by id
// @Tags         booking_setting
// @Accept       json
// @Produce      json
// @Param        id  path   int  true  "Booking Setting ID"
// @Success      200  {object}  map[string]interface{}
// @Router       /booking_setting/{id} [delete]
func (c *BookingSettingController) Delete() {
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "无效的ID"}
		c.ServeJSON()
		return
	}

	err := models.DeleteBookingSetting(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{"success": false, "message": "删除失败: " + err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{"success": true, "message": "删除成功"}
	c.ServeJSON()
}
